#pragma once
#include<iostream>
#include<functional>
#include<string>
#include"log.hpp"
#include<stdlib.h>
void serviceIO(int sock)
{
    char buffer[1024];
    while (true) // 收发需要循环，不然一次完成了对话，就关闭了
    {
        ssize_t n = read(sock, buffer, sizeof(buffer));
        if (n > 0)
        {
            // 目前我们把读到的数据当成字符串, 截止目前
            buffer[n] = 0;
            std::cout << "收到消息：" << buffer << std::endl;

            std::string outbuffer = buffer;
            outbuffer += " server[echo]";

            write(sock, outbuffer.c_str(), outbuffer.size()); // 多路转接
        }
        else if (n == 0)
        {
            // 代表client退出
            logMessage(NORMAL, "client quit, me too!");
            break;
        }
        close(sock);
    }
}
class Task
{ typedef std::function<void(int)> func_t;
public:
    Task(){}
    Task(int sock,func_t func=serviceIO):_sock(sock),_func(func)
    {
    }
    void operator()()
    {  _func(_sock);
    }
   ~Task()
   {}
private:
 int _sock;
 func_t _func;
};
  